﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DemoApp1.Common
{
    public class ContextService
    {
        public static class Context
        {
            /// <summary>
            /// 当前登录的User
            /// </summary>
            public static CWPrincipal CurrentUser
            {
                get
                {
                    if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        var user = System.Web.HttpContext.Current.User as CWPrincipal;
                        return user;
                    }

                    return null;
                }
            }

            public static string User
            {
                get
                {
                    if (CurrentUser != null)
                    {
                        return CurrentUser.UserName;
                    }

                    return null;
                }
            }

            public static string UserId
            {
                get
                {
                    //if (System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    //{
                    //    HttpCookie cookie = HttpContext.Current.Request.Cookies[GlobalResources.Label.ExtendedUserDataCookieName];
                    //    if (cookie != null)
                    //    {
                    //        string json = cookie.Value;
                    //        ExtendedUserData data = JsonHelper.JsonDeserialize<ExtendedUserData>(json);
                    //        return data.UserId;
                    //    }
                    //}
                    if (CurrentUser != null)
                    {
                        return CurrentUser.UserID.ToString();
                    }

                    return string.Empty;
                }
            }

            public static string Role
            {
                get
                {
                    //if (System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    //{
                    //    FormsIdentity identity = (FormsIdentity)System.Web.HttpContext.Current.User.Identity;
                    //    if (identity != null)
                    //    {
                    //        FormsAuthenticationTicket ticket = identity.Ticket;
                    //        string roles = ticket.UserData;
                    //        return roles.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)[0];
                    //    }
                    //}
                    if (CurrentUser != null && CurrentUser.Roles.Count > 0)
                    {
                        return CurrentUser.Roles[0];
                    }

                    return string.Empty;
                }
            }

            /// <summary>
            /// 框架支持用户有多重角色
            /// </summary>
            public static string[] Roles
            {
                get
                {
                    //if (System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    //{
                    //    FormsIdentity identity = (FormsIdentity)System.Web.HttpContext.Current.User.Identity;
                    //    if (identity != null)
                    //    {
                    //        FormsAuthenticationTicket ticket = identity.Ticket;
                    //        string roles = ticket.UserData;
                    //        return roles.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    //    }
                    //}

                    if (CurrentUser != null)
                    {
                        return CurrentUser.Roles.ToArray();
                    }
                    else
                    {
                        return null;
                    }

                }
            }

            /// <summary>
            /// 用户所属部门ID，下属部门ID
            /// </summary>
          

            public static string CurrentRootURL
            {
                get
                {
                    string url = string.Empty;
                    if (HttpContext.Current != null)
                    {
                        url = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
                        if (url.EndsWith("/"))
                        {
                            url = url.Substring(0, url.Length - 1);
                        }
                    }
                    return url;
                    //return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
                    //return HttpContext.Current.Server.MapPath("~/").TrimEnd('/');
                }
            }

            public static string CurrentApplicationPath
            {
                get
                {
                    string applicationPath = HttpContext.Current.Request.ApplicationPath;
                    if (applicationPath.Trim('/') == string.Empty)
                    {
                        return string.Empty;
                    }
                    else
                    {
                        return applicationPath;
                    }
                }
            }

            /// <summary>
            /// 当前系统可用的模块
            /// </summary>
            public static List<string> Modules
            {
                get;
                set;
            }

            public static string OfficialWebsite
            {
                get
                {
                    return System.Configuration.ConfigurationManager.AppSettings["OfficialWebsite"].ToString();
                }
            }

            public static string LoginRotatorImage1
            {
                get
                {
                    return string.Format("{0}/{1}", OfficialWebsite.TrimEnd('/'), "static/jwt1.jpg");
                }
            }

            public static string LoginRotatorImage2
            {
                get
                {
                    return string.Format("{0}/{1}", OfficialWebsite.TrimEnd('/'), "static/jwt2.jpg");
                }
            }

            public static string LoginRotatorImage3
            {
                get
                {
                    return string.Format("{0}/{1}", OfficialWebsite.TrimEnd('/'), "static/jwt3.jpg");
                }
            }
        }
    }
}